//
//  UIView+Extesion.swift
//  Teamo
//
//  Created by mdzz on 2021/12/6.
//

import Foundation
import UIKit

@objc public extension UIView {
     var left: CGFloat {
        get {
            return frame.origin.x
        }
        set(newLeft) {
            var frame = self.frame
            frame.origin.x = newLeft
            self.frame = frame
        }
    }

     var top: CGFloat {
        get {
            return frame.origin.y
        }

        set(newTop) {
            var frame = self.frame
            frame.origin.y = newTop
            self.frame = frame
        }
    }

     var width: CGFloat {
        get {
            return frame.size.width
        }
        set(newWidth) {
            var frame = self.frame
            frame.size.width = newWidth
            self.frame = frame
        }
    }

     var height: CGFloat {
        get {
            return frame.size.height
        }

        set(newHeight) {
            var frame = self.frame
            frame.size.height = newHeight
            self.frame = frame
        }
    }

     var right: CGFloat {
        get {
            return left + width
        }
        set(newX) {
            var frame = self.frame
            frame.origin.x = newX - frame.size.width
            self.frame = frame
        }
    }

     var bottom: CGFloat {
        get {
            return top + height
        }
        set(newBottom) {
            var frame: CGRect = self.frame
            frame.origin.y = newBottom - frame.size.height
            self.frame = frame
        }
    }

     var centerX: CGFloat {
        get {
            return center.x
        }

        set(newCenterX) {
            var center = self.center
            center.x = newCenterX
            self.center = center
        }
    }

     var centerY: CGFloat {
        get {
            return center.y
        }

        set(newCenterY) {
            var center = self.center
            center.y = newCenterY
            self.center = center
        }
    }
    
    /// return the view's current viewController
     var viewController: UIViewController? {
        var responder: UIResponder = self
        while let next = responder.next {
            if let vc = next as? UIViewController {
                return vc
            }
            responder = next
        }
        return nil
    }
}


extension UIView {
    /// Add shadow to view.
    /// - Parameters:
    ///   - color: shadow color (default is black).
    ///   - radius: shadow radius (default is 4).
    ///   - offset: shadow offset (default is .zero).
    ///   - opacity: shadow opacity (default is 0.5).
    @objc public func addShadow(_ color: UIColor = UIColor.black, radius: CGFloat = 4, offset: CGSize = .zero, opacity: Float = 0.5) {
        layer.shadowColor = color.cgColor
        layer.shadowOffset = offset
        layer.shadowRadius = radius
        layer.shadowOpacity = opacity
        layer.masksToBounds = false

        layer.shouldRasterize = true
        layer.rasterizationScale = UIScreen.main.scale
    }
    
    
    /// Add Corner to view.
    /// - Parameters:
    ///   - corners: corners
    ///   - radius: radius
    @objc public func addCorners(_ corners: UIRectCorner, radius: CGFloat) {
        let size = CGSize(width: radius, height: radius)
        let maskPath = UIBezierPath(roundedRect: bounds, byRoundingCorners: corners, cornerRadii: size)

        let shape = CAShapeLayer()
        shape.path = maskPath.cgPath
        layer.mask = shape
    }
    
    // 左到右颜色 渐变
    @objc public func addTransitionColorLeftToRight(startColor: UIColor, endColor: UIColor) {
        addTransitionColor(startColor: startColor, endColor: endColor, startPoint: CGPoint(x: 0, y: 0), endPoint: CGPoint(x: 1, y: 0))
    }
    
    
    // 上到下颜色 渐变
    @objc public func addTransitionColorTopToBottm(startColor: UIColor, endColor: UIColor) {
        addTransitionColor(startColor: startColor, endColor: endColor, startPoint: CGPoint(x: 0, y: 0), endPoint: CGPoint(x: 0, y: 1))
    }
    
    // 左上到右下颜色 倾斜渐变
    @objc public func addTransitionColorTiltLeftTopToRightBottm(startColor: UIColor, endColor: UIColor) {
        addTransitionColor(startColor: startColor, endColor: endColor, startPoint: CGPoint(x: 0, y: 0), endPoint: CGPoint(x: 1, y: 1))
    }
    
    // 右上到左下颜色 倾斜渐变
    @objc public func addTransitionColorTiltRightTopToLeftBottm(startColor: UIColor, endColor: UIColor) {
        addTransitionColor(startColor: startColor, endColor: endColor, startPoint: CGPoint(x: 1, y: 0), endPoint: CGPoint(x: 0, y: 1))
    }
    
    /**
     颜色渲染：
     startColor: 开始颜色
     endColor: 结束颜色
     startPoint: 开始位置
     endPoint: 结束位置
     */
    @objc public func addTransitionColor(startColor: UIColor, endColor: UIColor, startPoint: CGPoint, endPoint: CGPoint) {
        let gradientLayer = CAGradientLayer()
        gradientLayer.colors = [startColor.cgColor, endColor.cgColor]
        gradientLayer.locations = [0, 1]
        gradientLayer.startPoint = startPoint;
        gradientLayer.endPoint = endPoint;
        gradientLayer.frame = bounds;
        layer.insertSublayer(gradientLayer, at: 0)
    }

}
